package com.xiaotao.service.impl;

import com.alibaba.fastjson.JSON;
import com.xiaotao.entity.GainRecord;
import com.xiaotao.mapper.GainRecordMapper;
import com.xiaotao.service.SystemService;
import com.xiaotao.util.ResponseEnum;
import com.xiaotao.util.ResponseUtil;
import com.xiaotao.vo.TodayEarningVo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SystemServiceImpl implements SystemService {
    @Resource
    private RedisTemplate redisTemplateInit;
    @Resource
    private GainRecordMapper gainRecordMapper;
    @Override
    public ResponseUtil getMoney() {
        //查redis
        String jsonStr = (String)redisTemplateInit.opsForValue().get("todayEarnings");
        //转换对象，返回
        if (jsonStr==null || jsonStr.isEmpty()){
            return ResponseUtil.get(ResponseEnum.FAIL,"未查到今日数据");
        }
        TodayEarningVo todayEarningVo = JSON.parseObject(jsonStr, TodayEarningVo.class);
        return ResponseUtil.get(ResponseEnum.OK,todayEarningVo);
    }

    @Override
    public ResponseUtil getMoneyByDate() {
        //从数据库查信息，只查当月的
        List<GainRecord> list= gainRecordMapper.findAll();
        return list!=null?ResponseUtil.get(ResponseEnum.OK,list):
                ResponseUtil.get(ResponseEnum.FAIL);
    }
}
